﻿/*
department : beacon
write by   : wuweiwei
github     : www.github.com/flybirdsoft
date       : 2015/10/28
*/

(function(win){
	var wGate = function(options){
		/*options 暂时无用*/
		this.tId = -1;
		this.body = document.getElementsByTagName("body")[0];
		if(options != undefined)
		{
			this.options = options;
		}
		else
		{
			this.options = {};
		}

		this.options.speed = 50;
		this.options.intervalCount = 5;/*未使用*/
		this.options.intervalIndex = 0;/*未使用*/
		this.list = [];
		/*
		this.list[i]={
			options: {
				name : "",
				content : "" , 可以是ID or html
				direction : "toUp|toDown|toLeft|toRight",
				speed : "slow|normal|fast"
				}	
			DOM : null
		}
		*/
		this.currentOptions = {};
		this.currentName = "";
		this.currentDOM  = null;

	};

	wGate.prototype.createDOM = function(type){
		return document.createElement(type);
	};

	wGate.prototype.append = function(options){
		/*
		options={
			name : "",
			content : "" , 可以是dom or html
			direction : "toUp|toDown|toLeft|toRight",
			backgroundColor : "#FFF"
			speed : "slow|normal|fast"		
		}
		*/
		var object = {};
		var dom = null;
		object.options = options;
		dom = this.createDOM("div");

		if(typeof(options.content)=="string") /*html element*/
		{
			dom.innerHTML = options.content;
		}
		else
		{
			dom.innerHTML = options.content.innerHTML;
			options.content.innerHTML = "";
		}

		dom.style.position = "fixed";
		dom.style.height = "100%";
		dom.style.width  = "100%";
		dom.style.overflow = "auto";
		if(options.direction=="toLeft")
		{
			dom.style.left   = "100%";
			dom.style.top    = "0%";
		}
		else if(options.direction=="toRight")
		{
			dom.style.left   = "-100%";
			dom.style.top    = "0%";
		}
		else if(options.direction=="toUp")
		{
			dom.style.left   = "0%";
			dom.style.top    = "100%";
		}
		else if(options.direction=="toDown")
		{
			dom.style.left   = "0%";
			dom.style.top    = "-100%";
		}
		else  /*默认是toLeft*/
		{
			dom.style.left   = "100%";
			dom.style.top    = "0%";
			object.options.direction = "toLeft";		
		}
		dom.style.backgroundColor = options.backgroundColor || "#FFF";
		object.DOM = dom;
		this.list.push(object);
		dom.id = "wGate_"+options.name;
		this.body.appendChild(dom);
		if(options.speed!=undefined)
		{
			this.options.speed = options.speed;
		}
	};
	/*end function append*/

	wGate.prototype.show = function(name){
		var th = this;
		var i;
		this.currentName = name;
		if(this.tId != -1)
		{
			return;
		}
		
		for(i=0;i<this.list.length;i++)
		{
			if(this.list[i].options.name == this.currentName)
			{
				this.currentOptions = this.list[i].options;
				this.currentDOM     = this.list[i].DOM;
			}
		}

		this.tId = setInterval(function(){
			var direction = th.currentOptions.direction;
			if(direction=="toLeft")
			{
				th.toLeft();
			}
			else if(direction=="toRight")
			{
				th.toRight();
			}
			else if(direction=="toUp")
			{
				th.toUp();
			}
			else if(direction=="toDown")
			{
				th.toDown();
			}
		},this.options.speed);
	}
	/*end function show*/


	wGate.prototype.hide = function(name){
		var th = this;
		var i;
		if(this.tId != -1)
		{
			return;
		}

		if(name != undefined)
		{
			this.currentName = name;
			for(i=0;i<this.list.length;i++)
			{
				if(this.list[i].options.name == this.currentName)
				{
					this.currentOptions = this.list[i].options;
					this.currentDOM     = this.list[i].DOM;
				}
			}
		}
		this.currentName = "";
		this.tId = setInterval(function(){
			var direction = th.currentOptions.direction;
			if(direction=="toLeft")
			{
				th.leaveLeft();
			}
			else if(direction=="toRight")
			{
				th.leaveRight();
			}
			else if(direction=="toUp")
			{
				th.leaveUp();
			}
			else if(direction=="toDown")
			{
				th.leaveDown();
			}
		},this.options.speed);
	}
	/*end function show*/



	/*---------------------interval function------------------------*/
	wGate.prototype.toLeft = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.left).replace("%",""));
		if(v<=0)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.left = "0%";
			return;
		}
		v -= 20;
		dom.style.left = v+"%";
	}
	/*end function toLeft*/

	wGate.prototype.toRight = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.left).replace("%",""));
		if(v>=0)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.left = "0%";
			return;
		}
		v += 20;
		dom.style.left = v+"%";
	}
	/*end function toRight*/

	wGate.prototype.toDown = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.top).replace("%",""));
		if(v>=0)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.top = "0%";
			return;
		}
		v += 20;
		dom.style.top = v+"%";
	}
	/*end function toDown*/

	wGate.prototype.toUp = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.top).replace("%",""));
		if(v<=0)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.top = "0%";
			return;
		}
		v -= 20;
		dom.style.top = v+"%";
	}
	/*end function toUp*/
	/*---------------------end interval function------------------------*/

	/*---------------------interval function------------------------*/
	wGate.prototype.leaveLeft = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.left).replace("%",""));
		if(v>=100)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.left = "100%";
			return;
		}
		v += 20;
		dom.style.left = v+"%";
	}
	/*end function toLeft*/

	wGate.prototype.leaveRight = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.left).replace("%",""));
		if(v<=-100)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.left = "-100%";
			return;
		}
		v -= 20;
		dom.style.left = v+"%";
	}
	/*end function toRight*/

	wGate.prototype.leaveDown = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.top).replace("%",""));
		if(v<=-100)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.top = "-100%";
			return;
		}
		v -= 20;
		dom.style.top = v+"%";
	}
	/*end function toDown*/

	wGate.prototype.leaveUp = function(){
		var dom = this.currentDOM;
		var v = parseInt((dom.style.top).replace("%",""));
		if(v>=100)
		{
			clearInterval(this.tId);
			this.tId = -1;
			dom.style.top = "100%";
			return;
		}
		v += 20;
		dom.style.top = v+"%";
	}
	/*end function toUp*/
	/*---------------------end interval function------------------------*/

	wGate.prototype.delete = function(name){
		var i;
		for(i=0;i<this.list.length;i++)
		{
			if(this.list[i].options.name == name)
			{
				this.body.removeChild(this.list[i].DOM);
				this.list[i].DOM = null;				
				break;
			}
		}
		this.list.splice(i,1);
	}

	wGate.prototype.dispose = function(){
		var i;
		for(i=0;i<this.list.length;i++)
		{
			this.body.removeChild(this.list[i].DOM);
			this.list[i].DOM = null;
		}
		this.list.length = 0;
		this.options.speed = 50;
		this.currentOptions = {};
		this.currentName = "";
		this.currentDOM  = null;
	}
	/*end function dispose*/

	wGate.prototype.getName = function(){
		return {
			name : this.currentName ,
			id   : this.currentName == "" ? "" : "wGate_" + this.currentName
		}
	}

	win["wGate"] = wGate;
})(window);

/*
example:
	var gate = new wGate();
	gate.append({
		name:"gate1",
		content : "<h1>对方答复</h1>",
		direction : "toDown"
	});
	gate.show("gate1");
	$("h1").click(function(){
		gate.hide("gate1");
	});

*/